//
//  UIExtesionButton.m
//  TestDemo
//
//  Created by fengxijun on 3/9/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "UIExtesionButton.h"

@interface UIExtesionButton ()
@property (nonatomic,assign) NSTimeInterval beginTimeInterval;
@property (nonatomic,assign) NSTimer *timer;
@end

@implementation UIExtesionButton

@synthesize longPressButtonHandle=_longPressButtonHandle;
@synthesize tapButtonHandle=_tapButtonHandle;

-(instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setUp];
}

- (void)clearMemory
{
    [self stopTimer];
}

-(void)setUp
{
    self.longPressMonitorTimeInterval = 1.f;
    self.monitorLongPressInTouchUpInside = NO;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    [self registerAction];
}

-(void)registerAction
{
    [self addTarget:self action:@selector(tapEvent:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchDown];
}

-(void)removeFromSuperview
{
    [self clearMemory];
    [super removeFromSuperview];
}

-(void)tapEvent:(id)sender
{
}

- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event
{
    UITouch *anyTouch = [[event allTouches] anyObject];
    NSLog(@"%ld,%lf",(long)anyTouch.phase,anyTouch.timestamp);
    switch (anyTouch.phase) {
        case UITouchPhaseBegan:
        {
            self.beginTimeInterval = anyTouch.timestamp;
            if (!self.monitorLongPressInTouchUpInside) {
                [self startTimer];
            }
        }
            break;
        case UITouchPhaseEnded:
        {
            [self stopTimer];
            [self processTouchEventForwardingBeginTimestamp:self.beginTimeInterval endTimestamp:anyTouch.timestamp];
        }
            break;
        default:
            break;
    }
    [super sendAction:action to:target forEvent:event];
}

- (void)processTouchEventForwardingBeginTimestamp:(NSTimeInterval)beginTimestamp endTimestamp:(NSTimeInterval)endTimestamp
{
    if (self.longPressMonitorTimeInterval < (endTimestamp - beginTimestamp) && self.longPressButtonHandle) {
        self.longPressButtonHandle(self);
    } else if (self.tapButtonHandle) {
        self.tapButtonHandle(self);
    }
}

-(void)dealloc
{
    [self clearMemory];
}

-(void)startTimer
{
    [self stopTimer];
    _timer = [NSTimer scheduledTimerWithTimeInterval:self.longPressMonitorTimeInterval target:self selector:@selector(timerSchedule:) userInfo:nil repeats:NO];
}

-(void)timerSchedule:(NSTimer *)tm
{
    if (self.isTracking) {
        [self stopTimer];
        [self processTouchEventForwardingBeginTimestamp:self.beginTimeInterval endTimestamp:self.beginTimeInterval + self.longPressMonitorTimeInterval + 0.001];
    }
}

-(void)stopTimer
{
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
        }
        _timer = nil;
    }
}

@end
